package views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

import objects.Constituent;
import objects.Feature;

public class MainPanel extends JPanel {
	
	/** Data variables **/
	
	private boolean clicked;
	
	private Constituent constituent;
	private Constituent currCon;
	private String currentConstituent;
	private File currentFile;
	
	/** Component variables **/
	
	private JMenu mnFile;
	private JMenuItem mntmNew;
	private JMenuItem mntmLexicon;
	private JMenuItem mntmOntology;
	
	private JMenu mnViews;
	private JMenuItem mntmFeatures;
	private JMenuItem mntmOpen;
	private JMenuItem mntmSave;
	private JMenuItem mntmSaveAs;
	private JMenuItem mntmExit;
	
	private JMenu mnTools;
	private JMenuItem mntmNewLanguage;
	/*TODO private JMenuItem mntmSetup;*/
	
	private JPanel buttonsPanel;
	private JPanel sentencePanel;
	private JTextArea textSentence;
	private JLabel lblStatus;
	/* lblStatus can be used to show statuses like "Saved!" "Load successful!", etc. */
	
	private JTable table;
	private DefaultTableModel tableModel;
	
	private JButton btnOpen;
	private JButton btnSave;
	private JButton btnSaveAs;
	private JButton btnTranslate;
	private JButton btnDeleteFeature;
	private JButton btnAddFeature;
	private JButton btnEditConstituent;
	private JButton btnDeleteConstituent;
	
	private JLabel currentLabel;
	private JPanel currentPanel;
	
	public MainPanel() {
		clicked = false;
		setCurrentFile(null);
		sentencePanel = new JPanel();
		
		tableModel = new DefaultTableModel(
				new Object[][] {{null, null}, {null, null}, {null, null}, {null, null}, {null, null}},
				new String[] {"Name", "Value"}
		);
		
		setLayout(new BorderLayout(0, 0));
		setPreferredSize(new Dimension(885, 565));
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setPreferredSize(new Dimension(0, 30));
		add(menuBar, BorderLayout.NORTH);
		
		mnFile = new JMenu("File");
		mnFile.setMargin(new Insets(0, 0, 0, 10));
		menuBar.add(mnFile);
		
		mntmNew = new JMenuItem("New");
		mnFile.add(mntmNew);
		
		mntmOpen = new JMenuItem("Open File...");
		mnFile.add(mntmOpen);
		
		mntmSave = new JMenuItem("Save");
		mnFile.add(mntmSave);
		
		mntmSaveAs = new JMenuItem("Save As...");
		mnFile.add(mntmSaveAs);
		
		mnFile.addSeparator();
		
		mntmExit = new JMenuItem("Exit");
		mnFile.add(mntmExit);
		
		mnViews = new JMenu("Views");
		mnViews.setMargin(new Insets(0, 0, 0, 10));
		menuBar.add(mnViews);
		
		mntmFeatures = new JMenuItem("Features");
		mnViews.add(mntmFeatures);
		
		mntmLexicon = new JMenuItem("Lexicon");
		mnViews.add(mntmLexicon);
		
		mntmOntology = new JMenuItem("Ontology");
		mnViews.add(mntmOntology);
		
		mnTools = new JMenu("Tools");
		mnTools.setMargin(new Insets(0, 0, 0, 10));
		menuBar.add(mnTools);
		
		mntmNewLanguage = new JMenuItem("New Language");
		mnTools.add(mntmNewLanguage);
		
		/*TODO mnTools.addSeparator();
		
		mntmSetup = new JMenuItem("Setup");
		mnTools.add(mntmSetup);*/
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.75);
		add(splitPane, BorderLayout.CENTER);
		
		JPanel panCenter = new JPanel();
		splitPane.setLeftComponent(panCenter);
		panCenter.setLayout(new BorderLayout(0, 0));
		
		JPanel panTop = new JPanel();
		panCenter.add(panTop, BorderLayout.NORTH);
		FlowLayout flowLayout = (FlowLayout) panTop.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		
		btnOpen = new JButton("Open");
		btnOpen.setHorizontalAlignment(SwingConstants.LEFT);
		panTop.add(btnOpen);
		
		btnSave = new JButton("Save");
		panTop.add(btnSave);
		
		btnSaveAs = new JButton("Save As");
		panTop.add(btnSaveAs);
		
		btnTranslate = new JButton("Translate");
		panTop.add(btnTranslate);
		
		JSplitPane splitConcept = new JSplitPane();
		splitConcept.setResizeWeight(0.1);
		splitConcept.setOrientation(JSplitPane.VERTICAL_SPLIT);
		panCenter.add(splitConcept, BorderLayout.CENTER);
		
		JScrollPane scpConcepts = new JScrollPane();
		splitConcept.setLeftComponent(scpConcepts);
		scpConcepts.setMinimumSize(new Dimension(0, 80));
		scpConcepts.setPreferredSize(new Dimension(0, 80));
		scpConcepts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		
		sentencePanel = new JPanel();
		scpConcepts.setViewportView(sentencePanel);
		sentencePanel.setLayout(new WrapLayout());
		
		JSplitPane splitTranslate = new JSplitPane();
		splitTranslate.setResizeWeight(0.9);
		splitTranslate.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitConcept.setRightComponent(splitTranslate);
		
		JPanel panBottom = new JPanel();
		splitTranslate.setRightComponent(panBottom);
		panBottom.setLayout(new BorderLayout(0, 0));
		panBottom.setMinimumSize(new Dimension(0, 100));
		panBottom.setPreferredSize(new Dimension(0, 100));
		
		JLabel genLabel = new JLabel("Generated Text");
		genLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panBottom.add(genLabel, BorderLayout.NORTH);
		
		JScrollPane scpTranslation = new JScrollPane();
		panBottom.add(scpTranslation, BorderLayout.CENTER);
		scpTranslation.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		textSentence = new JTextArea();
		textSentence.setWrapStyleWord(true);
		textSentence.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textSentence.setEditable(false);
		textSentence.setRows(4);
		textSentence.setLineWrap(true);
		scpTranslation.setViewportView(textSentence);
		
		buttonsPanel = new JPanel();
		
		JScrollPane scpSemRep = new JScrollPane();
		splitTranslate.setLeftComponent(scpSemRep);
		scpSemRep.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scpSemRep.setPreferredSize(new Dimension(0, 100));
		scpSemRep.setMinimumSize(new Dimension(0, 100));
		
		buttonsPanel.setLayout(new WrapLayout());
		scpSemRep.setViewportView(buttonsPanel);
		
		JPanel panRight = new JPanel();
		splitPane.setRightComponent(panRight);
		panRight.setMinimumSize(new Dimension(200, 0));
		panRight.setPreferredSize(new Dimension(200, 0));
		panRight.setMaximumSize(new Dimension(300, 0));
		GridBagLayout gbl_panRight = new GridBagLayout();
		gbl_panRight.columnWidths = new int[]{233, 0};
		gbl_panRight.rowHeights = new int[] {36, 23, 23, 300, 23, 23, 17, 0};
		gbl_panRight.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panRight.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE, 0.0};
		panRight.setLayout(gbl_panRight);
		
		JPanel panTopSpace = new JPanel();
		GridBagConstraints gbc_panTopSpace = new GridBagConstraints();
		gbc_panTopSpace.fill = GridBagConstraints.BOTH;
		gbc_panTopSpace.insets = new Insets(0, 0, 5, 0);
		gbc_panTopSpace.gridx = 0;
		gbc_panTopSpace.gridy = 0;
		panRight.add(panTopSpace, gbc_panTopSpace);
		panTopSpace.setLayout(new BorderLayout(0, 0));
		
		lblStatus = new JLabel();
		lblStatus.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblStatus.setHorizontalAlignment(SwingConstants.CENTER);
		panTopSpace.add(lblStatus);
		
		btnEditConstituent = new JButton("Edit Constituent");
		GridBagConstraints gbc_btnEditConstituent = new GridBagConstraints();
		gbc_btnEditConstituent.fill = GridBagConstraints.BOTH;
		gbc_btnEditConstituent.insets = new Insets(0, 0, 5, 0);
		gbc_btnEditConstituent.gridx = 0;
		gbc_btnEditConstituent.gridy = 1;
		panRight.add(btnEditConstituent, gbc_btnEditConstituent);
		
		btnDeleteConstituent = new JButton("Delete Constituent");
		GridBagConstraints gbc_btnDeleteConstituent = new GridBagConstraints();
		gbc_btnDeleteConstituent.fill = GridBagConstraints.BOTH;
		gbc_btnDeleteConstituent.insets = new Insets(0, 0, 5, 0);
		gbc_btnDeleteConstituent.gridx = 0;
		gbc_btnDeleteConstituent.gridy = 2;
		panRight.add(btnDeleteConstituent, gbc_btnDeleteConstituent);
		
		JScrollPane scpFeatures = new JScrollPane();
		scpFeatures.setMinimumSize(new Dimension(195, 200));
		scpFeatures.setPreferredSize(new Dimension(195, 285));
		scpFeatures.setMaximumSize(new Dimension(295, 0));
		GridBagConstraints gbc_scpFeatures = new GridBagConstraints();
		gbc_scpFeatures.fill = GridBagConstraints.BOTH;
		gbc_scpFeatures.insets = new Insets(0, 0, 5, 0);
		gbc_scpFeatures.gridx = 0;
		gbc_scpFeatures.gridy = 3;
		panRight.add(scpFeatures, gbc_scpFeatures);
		
		table = new JTable(tableModel) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		
		scpFeatures.setViewportView(table);
		
		btnAddFeature = new JButton("Add Feature");
		GridBagConstraints gbc_btnAddFeature = new GridBagConstraints();
		gbc_btnAddFeature.fill = GridBagConstraints.BOTH;
		gbc_btnAddFeature.insets = new Insets(0, 0, 5, 0);
		gbc_btnAddFeature.gridx = 0;
		gbc_btnAddFeature.gridy = 4;
		panRight.add(btnAddFeature, gbc_btnAddFeature);
		
		btnDeleteFeature = new JButton("Delete Feature");
		GridBagConstraints gbc_btnDeleteFeature = new GridBagConstraints();
		gbc_btnDeleteFeature.fill = GridBagConstraints.BOTH;
		gbc_btnDeleteFeature.insets = new Insets(0, 0, 5, 0);
		gbc_btnDeleteFeature.gridx = 0;
		gbc_btnDeleteFeature.gridy = 5;
		panRight.add(btnDeleteFeature, gbc_btnDeleteFeature);
		
		JPanel panBottomSpace = new JPanel();
		GridBagConstraints gbc_panBottomSpace = new GridBagConstraints();
		gbc_panBottomSpace.fill = GridBagConstraints.BOTH;
		gbc_panBottomSpace.gridx = 0;
		gbc_panBottomSpace.gridy = 6;
		panRight.add(panBottomSpace, gbc_panBottomSpace);
		
		setUILook();
	}
	
	public void setUILook() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void loadFeature(Constituent con) {
		clearTable();
		setCurrentConstituent(con.getLabel());
		
		for (int i = 0;i < con.getFeatures().getFeatureList().size();i++) {
			addRow(new String[] {con.getFeatures().getFeatureList().get(i).getName(), con.getFeatures().getFeatureList().get(i).getValue()});
		}
	}
	
	public void loadDefaultFeature(ArrayList<Feature> featureList) {
		for(int i = 0; i < featureList.size(); i++) {
			addRow(new String[] {featureList.get(i).getName(), featureList.get(i).getValueList().get(0)});
		}
	}
	
	public void loadNoFeature() {
		if (tableModel.getRowCount() <= 0) {
			String message = "No features found";
			
			addRow(new String[] {message, message});
		}
	}
	
	public void loadSemantic(JPanel panel) {
		buttonsPanel.repaint();
		sentencePanel.repaint();
		
		addToSentencePanel(panel);
		revalidate();
	}
	
	public void setTextSentenceValue(String sentence) {
		textSentence.setText(sentence);
	}
	
	/** Label methods **/
	
	public void clearStatus() {
		lblStatus.setText("");
	}
	
	public void setStatus(String status) {
		lblStatus.setText(status);
	}
	
	/** Table methods **/
	
	public int getSelectedRow() {
		return table.getSelectedRow();
	}
	
	public int findColumn(String columnName) {
		return tableModel.findColumn(columnName);
	}
	
	public Object getValueAt(int row, int column) {
		return tableModel.getValueAt(row, column);
	}
	
	public void addRow(String[] value) {
		tableModel.addRow(value);
	}
	
	public void insertRow(int row, Object[] values) {
		tableModel.insertRow(row, values);
	}
	
	public void removeRow(int row) {
		tableModel.removeRow(row);
	}
	
	/** Clear fields **/
	
	public void clearGenerated() {
		textSentence.setText("");
	}
	
	public void clearPanels() {
		sentencePanel.removeAll();
		buttonsPanel.removeAll();
		textSentence.setText("");
		clearTable();
		
		sentencePanel.repaint();
		buttonsPanel.repaint();
	}

	public void clearTable() {
		while (tableModel.getRowCount() > 0) {
			tableModel.removeRow(0);
		}
	}
	
	/** Get or Set data **/
	
	public void setCurrCon(Constituent head) {
		this.currCon = head;
	}
	
	public Constituent getCurrCon() {
		return currCon;
	}
	
	public void setConstituent(Constituent con) {
		constituent = con;
	}
	
	public Constituent getConstituent() {
		return constituent;
	}
	
	public void setCurrentConstituent(String currentConstituent) {
		this.currentConstituent = currentConstituent;
	}
	
	public String getCurrentConstituent() {
		return currentConstituent;
	}
	
	public void setCurrentFile(File currentFile) {
		this.currentFile = currentFile;
	}
	
	public File getCurrentFile() {
		return currentFile;
	}
	
	public void setClicked(boolean clicked) {
		this.clicked = clicked;
	}
	
	public boolean isClicked() {
		return clicked;
	}
	
	/** Add to panel **/
	
	public void addToSentencePanel(JPanel panel) {
		this.sentencePanel.add(panel);
	}
	
	public void addToButtonsPanel(JLabel label) {
		this.buttonsPanel.add(label);
	}
	
	/** Get or set components **/
	
	public void setCurrentPanel(JPanel currentPanel) {
		this.currentPanel = currentPanel;
	}
	
	public void setCurrentLabel(JLabel currentLabel) {
		this.currentLabel = currentLabel;
	}
	
	public JPanel getCurrentPanel() {
		return currentPanel;
	}
	
	public JLabel getCurrentLabel() {
		return currentLabel;
	}
	
	public JPanel getSentencePanel() {
		return sentencePanel;
	}
	
	/** Add listeners **/
	
	public void addMenuNewSemRepListener(ActionListener listener) {
		mntmNew.addActionListener(listener);
	}
	
	public void addOpenListener(ActionListener listener) {
		mntmOpen.addActionListener(listener);
		btnOpen.addActionListener(listener);
	}
	
	public void addSaveListener(ActionListener listener) {
		mntmSave.addActionListener(listener);
		btnSave.addActionListener(listener);
	}
	
	public void addSaveAsListener(ActionListener listener) {
		mntmSaveAs.addActionListener(listener);
		btnSaveAs.addActionListener(listener);
	}
	
	public void addExitListener(ActionListener listener) {
		mntmExit.addActionListener(listener);
	}
	
	public void addMenuFeaturesListener(ActionListener listener) {
		mntmFeatures.addActionListener(listener);
	}
	
	public void addMenuLexiconListener(ActionListener listener) {
		mntmLexicon.addActionListener(listener);
	}
	
	public void addMenuOntologyListener(ActionListener listener) {
		mntmOntology.addActionListener(listener);
	}
	
	public void addMenuNewSemLangListener(ActionListener listener) {
		mntmNewLanguage.addActionListener(listener);
	}
	
	public void addTranslateListener(ActionListener listener) {
		btnTranslate.addActionListener(listener);
	}
	
	public void addAddFeatureListener(ActionListener listener) {
		btnAddFeature.addActionListener(listener);
	}
	
	public void addTableListener(MouseListener listener) {
		table.addMouseListener(listener);
	}
	
	public void addDeleteFeatureListener(ActionListener listener) {
		btnDeleteFeature.addActionListener(listener);
	}
	
	public void addEditConstituentListener(ActionListener listener) {
		btnEditConstituent.addActionListener(listener);
	}
	
	public void addDeleteConstituentListener(ActionListener listener) {
		btnDeleteConstituent.addActionListener(listener);
	}
}
